package arithmetic.leecode_arthmetic;


/**
 * 反转链表
 * describe:给你单链表的头节点 head ，请你反转链表，并返回反转后的链表。。
 * url:<a href="https://leetcode.cn/problems/reverse-linked-list/description/?envType=study-plan-v2&envId=top-100-liked"></a>
 */
public class Solution206 {


    public ListNode reverseList(ListNode head) {
        ListNode pre = new ListNode(0);
        pre.next = head;
        ListNode current = head;
        while (current!=null){
            ListNode next = current.next;
            current.next = pre; //这一步是指针反转
            pre = current; //移动pre变量 不改变链表指针
            current = next; // 移动current变量 不改变链表指针
        }
        return pre;
    }



    private class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
}
